package com.purchase.web.controller.common;

import com.purchase.common.annotation.Log;
import com.purchase.common.core.domain.AjaxResult;
import com.purchase.common.enums.BusinessType;
import com.purchase.common.utils.StringUtils;
import com.purchase.common.utils.file.FileUtils;
import com.purchase.common.utils.poi.ExcelUtil;
import com.purchase.manage.damian.DAO.StoreQuotationListEXCELDTO;
import com.purchase.manage.damian.OrderList;
import com.purchase.manage.damian.StoreQuotation;
import com.purchase.manage.service.IFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author oyi46
 * @version 1.0
 * Create by 2023/7/6 11:03
 */

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    IFileService fileService;

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    /**
     * 模板文件下载 之后可以改地址
     */
    @Log(title = "采购员模板文件下载 ", businessType = BusinessType.EXPORT)
    @PostMapping("/download")
    public void resourceDownload(String type, HttpServletResponse response) throws Exception {
        try {
            String downloadPath = null;
            if ("project".equals(type)) {
                downloadPath = "C:/purchase/downloadPath/project.xlsx";
            } else {
                downloadPath = "C:/purchase/downloadPath/store.xlsx";
            }

//            if ("project".equals(type)) {
//                downloadPath = "/root/purchase/project.xlsx";
//            } else {
//                downloadPath = "/root/purchase/store.xlsx";
//            }

            // 下载名称
            String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, downloadName);
            FileUtils.writeBytes(downloadPath, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    //采购员上传 商品类询价单
    @PreAuthorize("@ss.hasPermi('purchase:purchase:insert')")
    @Log(title = "采购员上传商品类询价单 ", businessType = BusinessType.IMPORT)
    @PostMapping("/upload/purchase/other")
    public AjaxResult upStorePurchase(OrderList orderList) throws Exception {
        MultipartFile file = orderList.getFile();
        if (file == null) {
            return AjaxResult.error("上传文件不存在");
        }
        fileService.poiReadPurchase(file, orderList);
        return AjaxResult.success("上传成功");
    }

    @PreAuthorize("@ss.hasPermi('supplier:supplier:supplier')")
    @Log(title = "供应商上传商品类询价单 ", businessType = BusinessType.IMPORT)
    @PostMapping("/upload/supplier/other")
    public AjaxResult upStoreSupplier(StoreQuotation storeQuotation) throws Exception {
        MultipartFile file = storeQuotation.getFile();
        MultipartFile photo = storeQuotation.getPhoto();
        if (file == null || photo == null) {
            return AjaxResult.error("上传文件不存在");
        }
        fileService.selectCountOrder(storeQuotation.getOrderId(), "商品类");
        fileService.poiReadSupplier(file, storeQuotation, photo);
        return AjaxResult.success();
    }

    //采购员上传 商品类询价单
    @PreAuthorize("@ss.hasPermi('purchase:purchase:insert')")
    @Log(title = "采购员上传工程类询价单 ", businessType = BusinessType.IMPORT)
    @PostMapping("/upload/purchase/project")
    public AjaxResult upProjectPurchase(OrderList orderList) throws Exception {
        MultipartFile file = orderList.getFile();
        if (file == null) {
            return AjaxResult.error("上传文件不存在");
        }
        fileService.poiReadPurchaseProject(file, orderList);
        return AjaxResult.success("上传成功");
    }

    @PreAuthorize("@ss.hasPermi('supplier:supplier:supplier')")
    @Log(title = "供应商上传工程类询价单 ", businessType = BusinessType.IMPORT)
    @PostMapping("/upload/supplier/project")
    public AjaxResult upProjectSupplier(StoreQuotation storeQuotation) throws Exception {
        MultipartFile file = storeQuotation.getFile();
        MultipartFile photo = storeQuotation.getPhoto();
        Long orderId = storeQuotation.getOrderId();
        if (file == null || photo == null) {
            return AjaxResult.error("上传文件不存在");
        }
        fileService.selectCountOrder(orderId, "工程类");
        return AjaxResult.success(fileService.poiReadSupplierProject(file, orderId, photo));
    }


    @PreAuthorize("@ss.hasPermi('supplier:supplier:supplier')")
    @Log(title = "供应商下载工程类询价单 ", businessType = BusinessType.EXPORT)
    @PostMapping("/download/supplier/project")
    public void supplierDownloadProject(Long orderId, HttpServletResponse response) throws Exception {
        fileService.supplierDownloadProject(orderId, response);
    }

    @PreAuthorize("@ss.hasPermi('supplier:supplier:supplier')")
    @Log(title = "供应商下载商品类询价单 ", businessType = BusinessType.EXPORT)
    @PostMapping("/download/supplier/store")
    public void supplierDownloadstore(Long orderId, HttpServletResponse response) throws Exception {
        fileService.supplierDownloadStore(orderId, response);
    }

    @PreAuthorize("@ss.hasPermi('purchase:purchase:insert')")
    @Log(title = "历史商品下载 ", businessType = BusinessType.EXPORT)
    @PostMapping("/download/supplierstore")
    public void supplierDownloadStore(HttpServletResponse response, String name) throws Exception {
        List<StoreQuotationListEXCELDTO> list = fileService.priceComparison(name);
        ExcelUtil<StoreQuotationListEXCELDTO> util = new ExcelUtil<>(StoreQuotationListEXCELDTO.class);
        util.exportExcel(response, list, "历史价格");
    }

    @PreAuthorize("@ss.hasPermi('purchase:purchase:insert')")
    @Log(title = "采购员下载工程订单 ", businessType = BusinessType.EXPORT)
    @PostMapping("/down/purchase/project")
    public void purchaseDownloadproject(HttpServletResponse response, Long orderId, Long supplierId) throws Exception {
        fileService.purchaseDownProject(orderId, supplierId, response);
    }

    @PreAuthorize("@ss.hasPermi('purchase:purchase:insert')")
    @Log(title = "采购员下载商品订单 ", businessType = BusinessType.EXPORT)
    @PostMapping("/down/purchase/store")
    public void purchaseDownloadstore(HttpServletResponse response, Long orderId, Long supplierId) throws Exception {
        fileService.purchaseDownStore(orderId, supplierId, response);
    }

    //-----------


    //   //测试 待删除
    @PostMapping("/upload/rrr")
    public void xx(HttpServletResponse response,String name) throws Exception {
        fileService.insertSupplierState();

    }
}
